package mygame;

import com.jme3.export.InputCapsule;
import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter;
import com.jme3.export.OutputCapsule;
import com.jme3.scene.Node;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class WorldStateNode extends Node {
    protected VoxelGrid voxelGrid = new VoxelGrid();

    public WorldStateNode() {}

    public WorldStateNode(String name) {
	super(name);
    }

    public VoxelGrid getVoxelGrid() {
	return voxelGrid;
    }
    
    public Node getHubNodeUnselectableConstruct() {
        return (Node)getChild(Constants.HUB_NODE_UNSELECTABLE_CONSTRUCT);
    }
    
    public void setHubNodeUnselectableConstruct(Node unselectableConstructHubNode) {
        attachChild(unselectableConstructHubNode);
    }
    
    public Node getHubNodeSelectable() {
        return (Node)getChild(Constants.HUB_NODE_SELECTABLE);
    }
    
    public void setHubNodeSelectable(Node selectableHubNode) {
        attachChild(selectableHubNode);
    }

    public Node getHubNodeSelectableConstruct() {
        return (Node)getHubNodeSelectable().getChild(Constants.HUB_NODE_SELECTABLE_CONSTRUCT);
    }

    public void setHubNodeSelectableConstruct(Node selectableConstructHubNode) {
        getHubNodeSelectable().attachChild(selectableConstructHubNode);
    }
    
    public Node getHubNodeCharacter() {
        return (Node)getHubNodeSelectable().getChild(Constants.HUB_NODE_CHARACTER);
    }
    
    public void setHubNodeCharacter(Node characterHubNode) {
        getHubNodeSelectable().attachChild(characterHubNode);
    }

    public List<Node> getAllConstructHubNodes() {
	List<Node> allConstructHubNodes = new ArrayList<Node>();
	allConstructHubNodes.add(getHubNodeUnselectableConstruct());
	allConstructHubNodes.add(getHubNodeSelectableConstruct());
	return allConstructHubNodes;
    }

    @Override
    public void write(JmeExporter ex) throws IOException {
	super.write(ex);
	OutputCapsule capsule = ex.getCapsule(this);
	capsule.write(voxelGrid, "voxelGrid", new VoxelGrid());
        capsule.write(DataStore.voxelWireframeNode, "voxelWireframeNode", null); //for debugging
    }

    @Override
    public void read(JmeImporter im) throws IOException {
	super.read(im);
	InputCapsule capsule = im.getCapsule(this);
	voxelGrid = (VoxelGrid)capsule.readSavable("voxelGrid", new VoxelGrid());
        DataStore.voxelWireframeNode = (Node)capsule.readSavable("voxelWireframeNode", null); //for debugging
        DataStore.rootNode.attachChild(DataStore.voxelWireframeNode); //for debugging
    }
}
